﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;
using System.Reflection;
using System.Xml;
using System.IO.Ports;

namespace KeaoDataService
{
    public partial class MainFrom : Form
    {
        private  BaseUpLoadInfo m_CurrentUpLoadInfo;

        public MainFrom()
        {
            InitializeComponent();
        }

        private void InitPorts()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string str in ports)
            {
                cmb_dtu.Items.Add(str);
            }
        }

        private void MainFrom_Load(object sender, EventArgs e)
        {
            string[] portStrs = SerialPort.GetPortNames();
            foreach (string str in portStrs)
            {
                cmb_dtu.Items.Add(str);
            }

            cmb_dtu.Text = KeaoUtils.ConfigGetNodeAttr("网络设备", "地址");
            if (KeaoUtils.ConfigGetNodeAttr("网络设备", "类型") == "KeaoNetwok")
            {
                lst_shoujihaoma.Enabled = false;
                cmb_dtu.Enabled = false;
                txt_shoujihaoma.Enabled = false;
                btn_shanchushoujihaoma.Enabled = false;
                btn_tianjiashoujihao.Enabled = false;
                lst_shoujihaoma.Items.Clear();
            }
            else
            {
                XmlNodeList shouJiHao = KeaoUtils.ConfigGetNodeListByTag("发送号码");
                foreach (XmlNode node in shouJiHao)
                {
                    lst_shoujihaoma.Items.Add(node.InnerText);
                }
            }
            txt_tixingshijian.Text = KeaoUtils.ConfigGetNodeAttr("时段", "提醒时间");
            chk_tixing.Checked = KeaoUtils.ConfigGetNodeAttr("时段", "日结提醒") == "启用" ? true : false;


            chk_1.Checked = KeaoUtils.ConfigGetNodeAttr("料仓1", "状态") == "启用" ? true : false;
            chk_2.Checked = KeaoUtils.ConfigGetNodeAttr("料仓2", "状态") == "启用" ? true : false;
            chk_3.Checked = KeaoUtils.ConfigGetNodeAttr("料仓3", "状态") == "启用" ? true : false;
            chk_4.Checked = KeaoUtils.ConfigGetNodeAttr("料仓4", "状态") == "启用" ? true : false;
            chk_5.Checked = KeaoUtils.ConfigGetNodeAttr("料仓5", "状态") == "启用" ? true : false;

            cmb_1.Text = KeaoUtils.ConfigGetNodeAttr("料仓1", "类型");
            cmb_2.Text = KeaoUtils.ConfigGetNodeAttr("料仓2", "类型");
            cmb_3.Text = KeaoUtils.ConfigGetNodeAttr("料仓3", "类型");
            cmb_4.Text = KeaoUtils.ConfigGetNodeAttr("料仓4", "类型");
            cmb_5.Text = KeaoUtils.ConfigGetNodeAttr("料仓5", "类型");

            m_CurrentUpLoadInfo = Assembly.Load("KeaoDataService").CreateInstance(string.Format("KeaoDataService.{0}", KeaoUtils.ConfigGetNodeAttr("系统配置", "上传类型"))) as BaseUpLoadInfo;
            if (!m_CurrentUpLoadInfo.Init())
            {
                CloseApplication();
            }

            if (m_CurrentUpLoadInfo.UpLoadStatus)
            {
                this.notifyIcon1.Text = "数据上传服务——已连接设备";
            }
            else
            {
                this.notifyIcon1.Text = "数据上传服务——连接失败";
            }
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            KeaoUtils.ConfigSetNodeAttr("料仓1", "状态", chk_1.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("料仓2", "状态", chk_2.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("料仓3", "状态", chk_3.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("料仓4", "状态", chk_4.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("料仓5", "状态", chk_5.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("料仓1", "类型", cmb_1.Text);
            KeaoUtils.ConfigSetNodeAttr("料仓2", "类型", cmb_2.Text);
            KeaoUtils.ConfigSetNodeAttr("料仓3", "类型", cmb_3.Text);
            KeaoUtils.ConfigSetNodeAttr("料仓4", "类型", cmb_4.Text);
            KeaoUtils.ConfigSetNodeAttr("料仓5", "类型", cmb_5.Text);
            KeaoUtils.ConfigSetNodeAttr("网络设备", "地址", cmb_dtu.Text);
            KeaoUtils.ConfigSetNodeAttr("网络设备", "状态", chk_tixing.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigRemoveAllChildNodes("网络设备");
            foreach (string str in lst_shoujihaoma.Items)
            {
                KeaoUtils.ConfigAddChildNodesInnerText("网络设备", "发送号码", str);
            }
            KeaoUtils.ConfigSetNodeAttr("时段", "提醒时间", txt_tixingshijian.Text);
            KeaoUtils.ConfigSetNodeAttr("时段", "日结提醒", chk_tixing.Checked ? "启用" : "禁用");

            if (!m_CurrentUpLoadInfo.UpLoadStatus)
            {
                m_CurrentUpLoadInfo.Close();
                notifyIcon1.Visible = false;
                notifyIcon1.Dispose();
                notifyIcon1 = null;
                Application.Restart();
                Application.ExitThread();
                Environment.Exit(0);
            }

        }

        private void MainFrom_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                this.WindowState = FormWindowState.Minimized;
                this.Visible = false;
                e.Cancel = true;
                return;
            }
        }

        private void btn_set_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            if (KeaoUtils.ShowQuestionMessage("确定要退出系统吗？") == DialogResult.Yes)
            {
                CloseApplication();
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void CloseApplication()
        {
            m_CurrentUpLoadInfo.Close();
            Application.ExitThread();
            notifyIcon1.Visible = false;
            notifyIcon1.Dispose();
            notifyIcon1 = null;
            Environment.Exit(0);
        }

        private void btn_shanchushoujihaoma_Click(object sender, EventArgs e)
        {
            if (lst_shoujihaoma.SelectedIndex >= 0)
            {
                lst_shoujihaoma.Items.RemoveAt(lst_shoujihaoma.SelectedIndex);
            }
        }

        private void btn_tianjiashoujihao_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txt_shoujihaoma.Text) && KeaoUtils.isPhoneNumber(txt_shoujihaoma.Text))
            {
                lst_shoujihaoma.Items.Remove(txt_shoujihaoma.Text);
                lst_shoujihaoma.Items.Add(txt_shoujihaoma.Text);
                txt_shoujihaoma.Text = string.Empty;
            }
        }

    }
}
